#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <netinet/in.h>

#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <iostream>
#include <sstream>
using namespace std;

#define PORT 15020 //15020 is preemption port //5799 is GID port
#define BROADCAST_ADDR "192.168.255.255"
#define RSU_ADDR "192.168.1.21" //"192.168.1.21" // the object ip


int main(int argc, char *argv[])
{
        int sockfd;
        int broadcast=1;
        struct sockaddr_in sendaddr;
        struct sockaddr_in recvaddr;
        int numbytes;
        char sendMSG[] = "PREEMPT_SET N";//"RAMP_SET 1";//"PREEMPT_SET N";
	    char sendMSG2[] = "PREEMPT_CLEAR N";//"RAMP_CLEAR 1";//"PREEMPT_CLEAR N";
	    char recvbuff[256] = "";
        int  recvbufflen = 256;



        if((sockfd = socket(PF_INET,SOCK_DGRAM,0)) == -1)
        {
                perror("sockfd");
                exit(1);
        }

       if((setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,
                                &broadcast,sizeof broadcast)) == -1)
        {
                perror("setsockopt - SO_SOCKET ");
                exit(1);
        }
 //Network is unreachable
        sendaddr.sin_family = AF_INET;
        sendaddr.sin_port = htons(PORT);
        sendaddr.sin_addr.s_addr = INADDR_ANY;//INADDR_ANY;inet_addr(RSU_ADDR)
        memset(sendaddr.sin_zero,'\0',sizeof sendaddr.sin_zero);

        if(bind(sockfd, (struct sockaddr*) &sendaddr, sizeof sendaddr) == -1)
        {
                perror("bind");
                exit(1);
        }

  // HQ Change
        recvaddr.sin_family = AF_INET;
        recvaddr.sin_port = htons(PORT);
        recvaddr.sin_addr.s_addr = inet_addr(BROADCAST_ADDR) ; //INADDR_BROADCAST;

        memset(recvaddr.sin_zero,'\0',sizeof recvaddr.sin_zero);

	int addr_length = sizeof ( recvaddr );

    int IsTenTimes=10;

   // while(IsTenTimes)
    while(1)
    {
           //cout<<"Send the messages$$  " <<IsTenTimes<<" $$times left..."<<endl;
           time_t TimeSec=time(NULL);
           ostringstream oss;
           oss<<TimeSec;

           string S(oss.str());

           string s = "Message from RSU AT time:";

           s+=S+"\n";
           const char *sendMSG=s.c_str();

                numbytes = sendto(sockfd, sendMSG,strlen(sendMSG)+1 , 0,(struct sockaddr *)&recvaddr, addr_length);

	            //if (numbytes>0)
   	         //   {
             //               printf("Sent a packet. length: %d \r\n",numbytes);

		           // recvfrom(sockfd,recvbuff,recvbufflen,0,(struct sockaddr *)&recvaddr,( socklen_t * ) &addr_length);

   		        //    printf( "\n\n\tReceived message from OBU is => %s \r\n",recvbuff);

             //               //sleep(1000);
   	         //   }
             //       else perror("sendto");

                    cout<<"Server  Address:"<< inet_ntoa(recvaddr.sin_addr)<<"   Port:  "<<ntohs(recvaddr.sin_port)<<endl
                        <<"Client  Address is:"<<inet_ntoa(sendaddr.sin_addr)<<"   Port:  "<<ntohs(sendaddr.sin_port)<<endl<<endl<<endl;

	            usleep(10000*1000);    // Wait 5 seconds

           //         numbytes = sendto(sockfd, sendMSG2,strlen(sendMSG2)+1 , 0,(struct sockaddr *)&recvaddr, addr_length);

	          //  if (numbytes>0)
   	       //     {
           //                 printf("Sent a packet. length: %d \r\n",numbytes);

		         //   recvfrom(sockfd,recvbuff,recvbufflen,0,(struct sockaddr *)&recvaddr,( socklen_t * ) &addr_length);

   		      //      printf( "\n\n\tReceived message from OBU is => %s \r\n",recvbuff);

           //                 //sleep(1000);
   	       //     }
           //         else perror("sendto");


          // IsTenTimes--;


    }   // While(1)

        //exit(1);
        close(sockfd);

        return 0;
}

